<?php
/**
 * @author fld 2021-06-14
 * @desc 管理员业务类
 */
namespace App\Service\Admin;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

class AdminService
{
    private $module_name = '管理员管理';

    public function __construct()
    {

    }

    /**
     * @author fld 2021-06-14
     * @desc 通过条件获取一条管理员数据
     * @return mixed
     */
    public function getAdminByCondition($condition){
        $admin = DB::table('admin')->where($condition)->first();
        return objectToArray($admin);
    }
    /**
     * @author fld 2021-06-14
     * @desc 根据id更新管理员信息
     * @param unknown $id
     * @param unknown $data
     * @return number
     */
    public function updateAdminById($id, $data){
        return DB::table('admin')->where('id', $id)->update($data);
    }
    /**
     * @author fld 2021-06-18
     * @desc 新增管理员信息
     * @param $data
     * @return int
     */
    public function addAdmin($data){
        return DB::table('admin')->insertGetId($data);
    }

    /**
     * @author fld 2021-06-17
     * @desc 获取管理员分页列表
     * @return array
     */
    public function getAdminList(){
        $params = request()->all();
        $condition = $this->getSearchCondition();
        $list = DB::table('admin')->where($condition)->paginate();
        $pager = $list->links();
        return ['list' => $list, 'pager' => $pager, 'params'=>$params];
    }

    /**
     * @author fld 2021-06-17
     * @desc 查询条件
     * @return array
     */
    public function getSearchCondition(){
        $condition = [];
        $params = request()->all();
        if(!empty($params['user_name'])){
            $condition[] = ['admin_name','like','%'.$params['user_name'].'%'];
        }
        if(!empty($params['true_name'])){
            $condition[] = ['true_name','like','%'.$params['true_name'].'%'];
        }
        if(!empty($params['phone'])){
            $condition[] = ['phone','like','%'.$params['phone'].'%'];
        }
        return $condition;
    }

    public function addOrUpdateAdmin(){
        $params = request()->all();
        if(empty(trim($params['name']))){
            return ['code' => 1,'message' => '用户名不能为空'];
        }
        if(strlen(trim($params['name'])) < 3){
            return ['code' => 1,'message' => '用户名长度必须大于等于3'];
        }
        if(empty($params['id'])){
            $admin_info = $this->getAdminByCondition(['admin_name'=>$params['name']]);
        }else{
            $condition[] = ['admin_name','=',$params['name']];
            $condition[] = ['id','!=',$params['id']];
            $admin_info = $this->getAdminByCondition($condition);
        }
        if(!empty($admin_info)){
            return ['code' => 1,'message' => '用户名已存在'];
        }
        if(empty($params['id']) && empty(trim($params['passwd']))){
            return ['code' => 1,'message' => '密码不能为空'];
        }
        if(empty(trim($params['true_name']))){
            return ['code' => 1,'message' => '姓名不能为空'];
        }
        if(empty(trim($params['role_id']))){
            return ['code' => 1,'message' => '角色权限不能为空'];
        }
        $insert_data = [];
        $insert_data['admin_name'] = trim($params['name']);
        if(empty($params['id'])){
            $insert_data['admin_pwd'] = encryptHashPassword(trim($params['passwd']));
        }
        $insert_data['true_name'] = trim($params['true_name']);
        $insert_data['phone'] = trim($params['phone']);
        $insert_data['role_id'] = intval($params['role_id']);
        if(empty($params['id'])){
            $insert_data['create_time'] = time();
            $res = $this->addAdmin($insert_data);
            if($res !== false){
                addActionLog(['module_name'=>$this->module_name,'type'=>'add','log_remark'=>'新增编号:'.$res.',用户名:'.trim($params['name']).'管理员信息']);
            }
        }else{
            $res = $this->updateAdminById($params['id'],$insert_data);
            if($res !== false){
                addActionLog(['module_name'=>$this->module_name,'type'=>'edit','log_remark'=>'修改编号:'.$params['id'].',用户名:'.trim($params['name']).'管理员信息']);
            }
        }
        if($res !== false){
            return ['code' => 0,'message' => '保存管理员成功'];
        }else{
            return ['code' => 1,'message' => '保存管理员失败'];
        }
    }

    /**
     * @author fld 2021-06-18
     * @desc 通过id删除管理员
     * @param $id
     * @return array
     */
    public function deleteAdminById($id){
        if($id == 1){
            return ['code' => 0,'message' => '无法删除该管理员'];
        }
        $admin = $this->getAdminByCondition(['id'=>$id]);
        $res = DB::table('admin')->where('id', $id)->delete();
        if($res !== false){
            addActionLog(['module_name'=>$this->module_name,'type'=>'delete','log_remark'=>'删除编号:'.$id.',用户名:'.$admin['admin_name'].'管理员信息']);
            return ['code' => 0,'message' => '删除管理员成功'];
        }else{
            return ['code' => 1,'message' => '删除管理员失败'];
        }
    }

    /**
     * @author fld 2021-06-21
     * @desc 修改管理员密码
     * @return array
     */
    public function resetAdminPwd(){
        $id = request()->post('id');
        $new_pwd = request()->post('new_pwd');
        $admin = $this->getAdminByCondition(['id'=>$id]);
        $res = $this->updateAdminById($id,['admin_pwd'=>encryptHashPassword($new_pwd)]);
        if($res !== false){
            addActionLog(['module_name'=>$this->module_name,'type'=>'edit','log_remark'=>'重置编号:'.$id.',用户名:'.$admin['admin_name'].'管理员密码']);
            return ['code' => 0,'message' => '修改密码成功'];
        }else{
            return ['code' => 1,'message' => '修改密码失败'];
        }
    }

    /**
     * @author fld 2021-06-23
     * @desc 通过条件获取管理员总条数
     * @return mixed
     */
    public function getAdminCountByCondition($condition){
        $count = DB::table('admin')->where($condition)->count();
        return !empty($count) ? $count : 0;
    }

    /**
     * @author fld 2021-07-19
     * @desc 更新密码
     * @return array
     */
    public function updatePwd(){
        $old_pwd = trim(request()->post('old_pwd'));
        $new_pwd = trim(request()->post('new_pwd'));
        $confirm_pwd = trim(request()->post('confirm_pwd'));

        if(empty($old_pwd)){
            return ['code' => 1,'message' => '旧密码不能为空'];
        }
        if(empty($new_pwd)){
            return ['code' => 1,'message' => '新密码不能为空'];
        }
        if(strlen($new_pwd) < 6){
            return ['code' => 1,'message' => '新密码长度不能小于6'];
        }
        if($new_pwd != $confirm_pwd){
            return ['code' => 1,'message' => '确认密码和新密码不一致'];
        }
        $admin_info = session('admin');
        $admin = $this->getAdminByCondition(['id'=>$admin_info['id']]);
        if(verifyHashPassword($old_pwd, $admin['admin_pwd'])){
            $res = $this->updateAdminById($admin_info['id'],['admin_pwd'=>encryptHashPassword($new_pwd)]);
            if($res !== false){
                addActionLog(['module_name'=>$this->module_name,'type'=>'edit','log_remark'=>'修改密码：编号:'.$admin_info['id'].',用户名:'.$admin['admin_name'].'密码']);
                return ['code' => 0,'message' => '修改密码成功'];
            }else{
                return ['code' => 1,'message' => '修改密码失败'];
            }
        }else{
            return ['code' => 1,'message' => '旧密码错误'];
        }
    }
}